package com.shenmazong.serverwebpoem.service.impl;

import com.shenmazong.serverwebpoem.mapper.IElasticsearchRepository;
import com.shenmazong.serverwebpoem.pojo.TbPoetry;
import com.shenmazong.serverwebpoem.service.ElasticSearchService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 田不平
 * @project server-web-poem
 * @date 2020/3/26 10:26 PM
 * http://www.shenmazong.com
 */

@Slf4j
@Service(value = "elasticService")
public class ElasticSearchServiceImpl implements ElasticSearchService {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchTemplate;

    @Autowired
    IElasticsearchRepository elasticRepository;

    /**
     * TODO 创建索引
     */
    @Override
    public void createIndex() {
        elasticsearchTemplate.createIndex(TbPoetry.class);
    }

    /**
     * TODO 删除索引
     * @param indexName
     */
    @Override
    public boolean deleteIndex(String indexName) {
        return elasticsearchTemplate.deleteIndex(indexName);
    }

    /**
     * TODO 存储单个文档
     * @param poetry
     */
    @Override
    public void saveDoc(TbPoetry poetry) {
        elasticRepository.save(poetry);
    }

    /**
     * TODO 存储多个文档
     * @param list
     */
    @Override
    public void saveAll(List<TbPoetry> list) {
        elasticRepository.saveAll(list);
    }

    /**
     * TODO 模糊查询
     * @param keyWord
     * @return
     */
    @Override
    public Object findText(String keyWord) {
        log.info("keyWord="+keyWord);
//        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(keyWord);
//        return elasticRepository.search(builder);

//        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(keyWord).field("title").defaultOperator(Operator.AND);
//        return elasticRepository.search(builder);

//        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(keyWord).field("content").defaultOperator(Operator.AND);
//        return elasticRepository.search(builder);

//        QueryStringQueryBuilder builder = new QueryStringQueryBuilder("\""+keyWord+"\"").field("title").defaultOperator(Operator.AND);
//        return elasticRepository.search(builder);

//        QueryBuilder builder = QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("title", keyWord)).boost(2.0f);
//        return elasticRepository.search(builder);

        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(keyWord)
                .field("title")
                .field("content")
                .field("author")
                .field("dynasty")
                .defaultOperator(Operator.AND);

        return elasticRepository.search(builder);
    }

    @Override
    public Page<TbPoetry> findByWord(String keyWord, Pageable pageable) {

        // 如果关键字为空，则查询所有的数据
        if(keyWord.equals("")) {
            return elasticRepository.findAll(pageable);
        }

        //Pageable pageable = PageRequest.of(0,10);
        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(keyWord)
                .field("title")
                .field("content")
                .field("author")
                .field("dynasty")
                .defaultOperator(Operator.AND);

        return elasticRepository.search(builder, pageable);
    }
}
